package com.pdsu.alumni.service.impl;

import com.pdsu.alumni.pojo.SensitiveWords;
import com.pdsu.alumni.service.IESService;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: alumni
 * @description:
 * @author: Acerola
 * @create: 2022-03-16 00:14
 **/
@Service
public class ESServiceImpl implements IESService {
    @Autowired
    private ElasticsearchRestTemplate template;

    @Override
    public List<String> getAnalyzes(String keyword) {
        AnalyzeResponse analyzeResponse = template.execute(
                new ElasticsearchRestTemplate.ClientCallback<AnalyzeResponse>() {
                    @Override
                    public AnalyzeResponse doWithClient(RestHighLevelClient client) throws IOException {
                        AnalyzeRequest analyzeRequest = AnalyzeRequest.withGlobalAnalyzer("ik_smart", keyword);
                        return client.indices().analyze(analyzeRequest, RequestOptions.DEFAULT);
                    }
                }
        );
        ArrayList<String> result = new ArrayList<>();
        analyzeResponse.getTokens().forEach(analyzeToken -> {
            result.add(analyzeToken.getTerm());
        });
        return result;
    }

    @Override
    public void save(SensitiveWords sensitiveWords) {
        template.save(sensitiveWords);
    }


    @Override
    public void saveAll(List<SensitiveWords> list) {
        
        template.save(list);
    }

    @Override
    public List<SensitiveWords> query(String analyze) {
        ArrayList<SensitiveWords> sensitiveWords = new ArrayList<>();
        TermQueryBuilder builder = QueryBuilders.termQuery("keyword.keyword", analyze);
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(builder)
                .build();
        SearchHits<SensitiveWords> search = template.search(query, SensitiveWords.class);
        System.out.println(search);
        for (SearchHit<SensitiveWords> sensitiveWordsSearchHit : search) {
            sensitiveWords.add(sensitiveWordsSearchHit.getContent());
            System.out.println(sensitiveWordsSearchHit.getContent().getKeyword());
        }
        return sensitiveWords;
    }

    @Override
    public void delete(SensitiveWords sensitiveWords) {
        template.delete(sensitiveWords);
    }

    //TODO 更新需要做
    @Override
    public void update(SensitiveWords sensitiveWords) {

    }
}
